package com.sxtyxt.online;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sxtyxt.entity.MailFile;
import com.sxtyxt.entity.ResultVo;
import com.sxtyxt.online.file.ActAddFileDetail;
import com.sxtyxt.online.file.ActFileDetail;
import com.sxtyxt.online.file.SendFileListAdapter;
import com.sxtyxt.pullrefresh.ui.PullToRefreshBase;
import com.sxtyxt.pullrefresh.ui.PullToRefreshListView;
import com.sxtyxt.util.GsonUtil;
import com.sxtyxt.volley.LoadingDialogResultListenerImpl;
import com.sxtyxt.volley.URL;
import com.sxtyxt.volley.VolleyUtils;
import com.sxtyxt.widget.MyCustomTitleBar;

/**
 * 分类fragment
 *
 * @author General
 */
public class ActSendFiles extends ActBase implements PullToRefreshBase.OnRefreshListener<View>, AdapterView.OnItemClickListener {
    private PullToRefreshListView mPullListView;
    private ListView lv;
    private SendFileListAdapter adapter;
    private TextView txtNoData;

    private MyCustomTitleBar mTitleBar;

    private int currentPage = 1;

    @Override
    protected void initUI() {
        super.initUI();

        mTitleBar = (MyCustomTitleBar) findViewById(R.id.title_bar);

        mTitleBar.mSetOnRightButtonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transfer(ActAddFileDetail.class, 1, "memberId");
            }
        });

        mPullListView = (PullToRefreshListView) findViewById(R.id.pullrefresh_lv);
        mPullListView.setPullLoadEnabled(true);
        mPullListView.setScrollLoadEnabled(false);
        mPullListView.setOnRefreshListener(this);
        lv = (ListView) mPullListView.getRefreshableView().findViewById(R.id.lv);
        txtNoData = (TextView) mPullListView.getRefreshableView().findViewById(R.id.tv_nodata);
        adapter = new SendFileListAdapter(this);


        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        requestService();
    }


    /**
     * 请求接口
     */
    private void requestService() {

        // 父节点传0
        VolleyUtils.requestService(SystemConst.SEND_FILE_LIST_URL, URL.getSendFileListParams(getLoginUserId(),currentPage,1),
                new LoadingDialogResultListenerImpl(this, "正在加载数据...") {

                    @Override
                    public void onSuccess(String response) {
                        super.onSuccess(response);
                        mPullListView.onPullDownRefreshComplete();
                        mPullListView.onPullUpRefreshComplete();
                        MailFileVO vo = GsonUtil.deser(response, MailFileVO.class);

                        if (vo == null) {
                            doToast(R.string.msg_wso_error);
                            return;
                        }
                        if (vo.getResult() == 1) {
                            if (currentPage == 1) {
                                adapter.clearListData();
                            }

                            if (vo.getList() != null && vo.getList().size() > 0) {
                                //vo.getList()
                                adapter.addListData(vo.getList());
                                adapter.notifyDataSetChanged();
                                txtNoData.setVisibility(View.GONE);
                                currentPage++;
                            }else {
                                mPullListView.setPullLoadEnabled(false);
                                if (adapter.getListData().size() <= 0) {
                                    txtNoData.setVisibility(View.VISIBLE);
                                } else {
                                    txtNoData.setVisibility(View.INVISIBLE);
                                }
                            }

                        } else {
                            doToast(vo.getMsg());
                        }

                    }

                    @Override
                    public void onError() {
                        super.onError();
                        mPullListView.onPullDownRefreshComplete();
                        mPullListView.onPullUpRefreshComplete();
                    }

                });


    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        MailFile mailFile = (MailFile) adapterView.getAdapter().getItem(i);
        if (mailFile != null) {
            transfer(ActFileDetail.class, mailFile.getPkId() + "", "fileId");
        } else {
            doToast("文件不存在");
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<View> refreshView) {
        currentPage = 1;
        mPullListView.setPullLoadEnabled(true);
        requestService();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<View> refreshView) {
        requestService();
    }


    class MailFileVO extends ResultVo<MailFile> {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.file_send;
    }


}
